<?php
namespace app\admin\controller;

/**
 * 管理员管理
 * Class User
 */
class UserController extends BaseController {

	protected function _initialize() {
		parent::_initialize();
		if (session('ADMIN.id') != 1) {
			$this->error('没有权限');
		}
	}

	/**
	 * 管理员管理
	 * @return mixed
	 */
	public function index() {
		$user_list = db('user u')->field('u.*,g.title')->join('auth_group_access a', 'u.id=a.uid')->join('auth_group g', 'a.group_id=g.id')->order('add_time desc')->paginate();
		return $this->fetch('index', array('user_list' => $user_list));
	}

	/**
	 * 保存管理员
	 * @param $group_id
	 */
	public function add() {
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$auth_group_access['group_id'] = $data['group_id'];
			unset($data['group_id']);
			$validate = $this->validate($data, 'User');
			if ($validate !== true) {
				$this->error($validate);
			}
			$data['add_time'] = time();
			$data['login_pwd'] = md5_salt($data['login_pwd']);
			$uid = db('user')->insertGetId($data);
			if ($uid) {
				$auth_group_access['uid'] = $uid;
				db('auth_group_access')->insert($auth_group_access);
				$this->success('添加成功');
			} else {
				$this->error('添加失败');
			}
		} else {
			$auth_group_list = db('auth_group')->select();
			return $this->fetch('add', array('auth_group_list' => $auth_group_list));
		}
	}

	/**
	 * 更新管理员
	 * @param $id
	 * @param $group_id
	 */
	public function update($id, $group_id = '') {
		$user = db('user')->find($id);
		if ($this->request->isPost()) {
			$data = $this->request->post();
			if ($id == 1 && $data['status'] != 1) {
				$this->error('默认管理员不可禁用');
			}
			$auth_group_access['group_id'] = $data['group_id'];
			unset($data['group_id']);
			if ($data['login_pwd']) {
				$data['login_pwd'] = md5_salt($data['login_pwd']);
			} else {
				$data['login_pwd'] = $user['login_pwd'];
			}
			$validate = $this->validate($data, 'User');
			if ($validate !== true) {
				$this->error($validate);
			}
			if (db('user')->update($data, $id) !== false) {
				db('auth_group_access')->where('uid', $id)->update($auth_group_access);
				$this->success('保存成功');
			} else {
				$this->error('保存失败');
			}
		} else {
			$auth_group_list = db('auth_group')->select();
			$auth_group_access = db('auth_group_access')->where('uid', $id)->find();
			$user['group_id'] = $auth_group_access['group_id'];
			return $this->fetch('update', array(
				'user' => $user,
				'auth_group_list' => $auth_group_list
			));
		}
	}

	/**
	 * 状态设置
	 * @return mixed
	 */
	public function status() {
		$ids = input('param.ids/a') ? input('param.ids/a') : input('param.id/a');
		if (in_array(1, $ids)) {
			$this->error('默认管理员无法设置');
		}
		$val = input('param.val');
		if ($val == false) {
			$val = 2;
		} else {
			$val = 1;
		}
		$res = db('user')->where('id', 'in', $ids)->setField('status', $val);
		if ($res === false) {
			$this->error('设置失败');
		}
		$this->success('设置成功');
	}

	/**
	 * 删除用户
	 * @param $id
	 */
	public function delete() {
		$ids = input('param.ids/a') ? input('param.ids/a') : input('param.id/a');
		if (in_array(1, $ids)) {
			$this->error('默认管理员无法删除');
		}
		// 获取主键
		$res = db('user')->where('id', 'in', $ids)->delete();
		// 删除关联表
		$res = db('auth_group_access')->where('uid', 'in', $ids)->delete();
		if ($res === false) {
			$this->error('删除失败');
		}
		$this->success('删除成功');
	}

}
